/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.dic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.dic.TimeQuantum;
import com.boxin.ims.modules.wechat.service.dic.TimeQuantumService;

/**
 * 时间段Controller
 * @author Jakemanse
 * @version 2013-12-16
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/dic/timeQuantum")
public class TimeQuantumController extends BaseController {

	@Autowired
	private TimeQuantumService timeQuantumService;
	
	@ModelAttribute
	public TimeQuantum get(@RequestParam(required=false) Long id) {
		if (id != null){
			return timeQuantumService.get(id);
		}else{
			return new TimeQuantum();
		}
	}
	
	@RequiresPermissions("wechat:dic:timeQuantum:view")
	@RequestMapping(value = {"list", ""})
	public String list(TimeQuantum timeQuantum, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			timeQuantum.setUser(user);
		}
        Page<TimeQuantum> page = timeQuantumService.find(new Page<TimeQuantum>(request, response), timeQuantum); 
        model.addAttribute("page", page);
		return "modules/wechat/dic/timeQuantumList";
	}

	@RequiresPermissions("wechat:dic:timeQuantum:view")
	@RequestMapping(value = "form")
	public String form(TimeQuantum timeQuantum, Model model) {
	
		User user = UserUtils.getUser();
		if (timeQuantum.getUser() == null || timeQuantum.getUser().getId() <1){
			timeQuantum.setUser(user);
		}
		
		model.addAttribute("timeQuantum", timeQuantum);
		return "modules/wechat/dic/timeQuantumForm";
	}

	@RequiresPermissions("wechat:dic:timeQuantum:edit")
	@RequestMapping(value = "save")
	public String save(TimeQuantum timeQuantum, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, timeQuantum)){
			return form(timeQuantum, model);
		}
		timeQuantumService.save(timeQuantum);
		addMessage(redirectAttributes, "保存时间段'" + timeQuantum.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/dic/timeQuantum/?repage";
	}
	
	@RequiresPermissions("wechat:dic:timeQuantum:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		timeQuantumService.delete(id);
		addMessage(redirectAttributes, "删除时间段成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/dic/timeQuantum/?repage";
	}

}
